'use strict';

const Controller = require('egg').Controller;
const lodash = require('lodash');

class SysMenuController extends Controller {
    /**
     * 获取列表
     */
    async querylist() {
        const { ctx } = this;
        const { queryParams } = ctx.request.body;
        let data = await ctx.service.sysMenu.querylist(queryParams);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: { tabledata: data }
        };
    }

    /**
     * 新增
     */
    async add() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body;
        //验证必填项
        await app.utils.validator.required(['menu_name', 'url', 'menu_type', 'visible', 'perms'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['menu_name', 'parent_id', 'order_num', 'url', 'menu_type', 'visible', 'perms', 'icon', 'remark'], submitBody);

        //添加组件值
        let menu_id_uuid = await app.utils.uuid.v1();
        submitData.menu_id = menu_id_uuid;

        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.create_by = userid;
        submitData.create_time = new Date();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //设置 删除标记
        submitData.deleted = "0";
        //保存
        let rs = await ctx.service.sysMenu.create(submitData);
        ctx.body = {
            code: 200,
            msg: "保存成功",
            data: {},
        };
    }

    /**
     * 编辑
     */
    async edit() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body.subData;
        //验证主键是否存在
        await app.utils.validator.required(['menu_id'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['menu_name', 'parent_id', 'order_num', 'url', 'menu_type', 'visible', 'perms', 'icon', 'remark'], submitBody);
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        console.log("编辑数据",submitData, { where: { menu_id: submitBody.menu_id } })
        let rs = await ctx.service.sysMenu.update(submitData, submitBody.menu_id);
        ctx.body = {
            code: 200,
            msg: "编辑成功",
            data: {},
        };
    }

    /**
     * 删除
     */
    async delete() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(['menu_id'], submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['menu_id'], submitBody);

        //操作相关信息
        //let userid = await ctx.service.auth.getColonyUserId();
        //submitData.update_by = userid;
        //submitData.update_time = new Date();
        //删除
        let rs = await ctx.service.sysMenu.delete(submitData.menu_id);
        ctx.body = {
            code: 200,
            msg: "删除成功",
            data: {},
        };
    }

    /**
     * 通过主键查询
     */
    async querybyid() {
        const { ctx, app } = this;
        console.log("提交的数据", ctx.request.body);
        let { menu_id } = ctx.request.body;

        //查询
        let rs = await ctx.service.sysMenu.queryById(menu_id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }
    /**
     * 查询菜单节点树
     */
    async queryTreeList() {
        const { ctx, app } = this;
        //查询
        let data = await ctx.service.sysMenu.queryTreeList();
        ctx.body = {
            code: 200,
            msg: "成功",
            data: { menuTreeData: data },
        };
    }

}

module.exports = SysMenuController;